package com.zjc.yohoshop.mvp.view.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.makeramen.roundedimageview.RoundedImageView;
import com.zjc.lib_core.base.BaseFragment;
import com.zjc.yohoshop.R;
import com.zjc.yohoshop.bean.CouponListEntity;
import com.zjc.yohoshop.bean.HomeBannerEntity;
import com.zjc.yohoshop.bean.HomeGoodsEntity;
import com.zjc.yohoshop.bean.HomeRecommendEntity;
import com.zjc.yohoshop.bean.UpgradeEntity;
import com.zjc.yohoshop.dao.USerService;
import com.zjc.yohoshop.di.compont.DaggerHomeFragmentCompont;
import com.zjc.yohoshop.di.modules.HomeFragmentModules;
import com.zjc.yohoshop.mvp.contract.HomeFragmentContract;
import com.zjc.yohoshop.mvp.persenter.HomeFragmentPresenter;
import com.zjc.yohoshop.mvc.view.FragmentActivity;
import com.zjc.yohoshop.mvp.view.adapter.MineAdapter;
import com.zjc.yohoshop.network.API;

/**
 * 我的界面
 */
public class MineFragment extends BaseFragment<HomeFragmentPresenter> implements HomeFragmentContract.HomeFragmentIView, View.OnClickListener {
    private String user_id = "";
    private LinearLayout ll_mine;
    private RecyclerView rv_mine;
    private MineAdapter mineAdapter;
    private ImageView iv_mine_message;
    private ImageView iv_mine_qr;
    private ImageView iv_mine_setting;

    private int alphaHeight = 150;
    private int totalY = 0;

    private Button btn_item_mine_head_login;
    private CardView cv_item_mine_head;
    private RoundedImageView iv_item_mine_head_user_icon;
    private TextView tv_item_mine_head_user_nick;
    private RelativeLayout rl_item_mine_head_create_order;
    private ImageView iv_item_mine_head_big;
    private TextView tv_item_mine_head_share_cash;
    private TextView tv_item_mine_head_login_details;
    private TextView tv_item_mine_head_had_num;
    private TextView tv_item_mine_head_my_income;
    private TextView tv_item_mine_head_yhj;
    private TextView tv_item_mine_head_yhb;
    private TextView tv_item_mine_head_xgm;
    private TextView tv_item_mine_head_lpk;
    private TextView tv_item_mine_head_spsc;
    private TextView tv_item_mine_head_dpsc;
    private TextView tv_item_mine_head_zxsc;
    private TextView tv_item_mine_head_lljl;
    private RelativeLayout rl_item_mine_head_top;
    private View view;
    private View view_head;

    @Override
    public View createView(LayoutInflater inflater) {
        view = inflater.inflate(R.layout.fragment_mine, null);
        return view;
    }

    @Override
    public void init(View view) {
        ll_mine = view.findViewById(R.id.ll_mine);
        rv_mine = view.findViewById(R.id.rv_mine);
        iv_mine_message = view.findViewById(R.id.iv_mine_message);
        iv_mine_qr = view.findViewById(R.id.iv_mine_qr);
        iv_mine_setting = view.findViewById(R.id.iv_mine_setting);

        //相关控件初始化
        view_head = View.inflate(getActivity(), R.layout.item_mine_head, null);
        cv_item_mine_head = view_head.findViewById(R.id.cv_item_mine_head);
        btn_item_mine_head_login = view_head.findViewById(R.id.btn_item_mine_head_login);
        iv_item_mine_head_user_icon = view_head.findViewById(R.id.iv_item_mine_head_user_icon);
        tv_item_mine_head_user_nick = view_head.findViewById(R.id.tv_item_mine_head_user_nick);
        rl_item_mine_head_create_order = view_head.findViewById(R.id.rl_item_mine_head_create_order);
        tv_item_mine_head_share_cash = view_head.findViewById(R.id.tv_item_mine_head_share_cash);
        tv_item_mine_head_login_details = view_head.findViewById(R.id.tv_item_mine_head_login_details);
        tv_item_mine_head_had_num = view_head.findViewById(R.id.tv_item_mine_head_had_num);
        tv_item_mine_head_my_income = view_head.findViewById(R.id.tv_item_mine_head_my_income);
        tv_item_mine_head_yhj = view_head.findViewById(R.id.tv_item_mine_head_yhj);
        tv_item_mine_head_yhb = view_head.findViewById(R.id.tv_item_mine_head_yhb);
        tv_item_mine_head_xgm = view_head.findViewById(R.id.tv_item_mine_head_xgm);
        tv_item_mine_head_lpk = view_head.findViewById(R.id.tv_item_mine_head_lpk);
        tv_item_mine_head_spsc = view_head.findViewById(R.id.tv_item_mine_head_spsc);//商品收藏数量
        tv_item_mine_head_dpsc = view_head.findViewById(R.id.tv_item_mine_head_dpsc);
        tv_item_mine_head_zxsc = view_head.findViewById(R.id.tv_item_mine_head_zxsc);
        tv_item_mine_head_lljl = view_head.findViewById(R.id.tv_item_mine_head_lljl);
        rl_item_mine_head_top = view_head.findViewById(R.id.rl_item_mine_head_top);

        //recyclerView配置
        mineAdapter = new MineAdapter(R.layout.item_mine);

        mineAdapter.addHeaderView(view_head, 0);
        rv_mine.setLayoutManager(new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false));
        rv_mine.setAdapter(mineAdapter);


    }

    @Override
    public void initData() {
        p.mine_request();
    }

    @Override
    public void setupDagger() {
        DaggerHomeFragmentCompont.builder().homeFragmentModules(new HomeFragmentModules(this))
                .build().mine_inject(this);
    }

    @Override
    public void initListener() {
        iv_mine_message.setOnClickListener(this);
        iv_mine_qr.setOnClickListener(this);
        iv_mine_setting.setOnClickListener(this);

        //底部数据点击事件
        mineAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent goodsValues = new Intent(getActivity(), FragmentActivity.class);
                goodsValues.putExtra("type", "goods_values");
                goodsValues.putExtra("goods_id", mineAdapter.getData().get(position).goods_id);
                getActivity().startActivity(goodsValues);
            }
        });

        //滑动监听
        rv_mine.addOnScrollListener(new HomeRecyclerListener());


        btn_item_mine_head_login.setOnClickListener(this);
        iv_item_mine_head_user_icon.setOnClickListener(this);
        tv_item_mine_head_user_nick.setOnClickListener(this);
        rl_item_mine_head_create_order.setOnClickListener(this);
        tv_item_mine_head_share_cash.setOnClickListener(this);
        tv_item_mine_head_login_details.setOnClickListener(this);
        tv_item_mine_head_had_num.setOnClickListener(this);
        tv_item_mine_head_my_income.setOnClickListener(this);
        view_head.findViewById(R.id.ll_item_mine_head_yfk).setOnClickListener(this);
        view_head.findViewById(R.id.ll_item_mine_head_dfh).setOnClickListener(this);
        view_head.findViewById(R.id.ll_item_mine_head_dsh).setOnClickListener(this);
        view_head.findViewById(R.id.ll_item_mine_head_sdyl).setOnClickListener(this);
        view_head.findViewById(R.id.ll_item_mine_head_thh).setOnClickListener(this);
        view_head.findViewById(R.id.ll_item_mine_head_yhj).setOnClickListener(this);
        view_head.findViewById(R.id.ll_item_mine_head_yhb).setOnClickListener(this);
        view_head.findViewById(R.id.ll_item_mine_head_xgm).setOnClickListener(this);
        view_head.findViewById(R.id.ll_item_mine_head_lpk).setOnClickListener(this);
        view_head.findViewById(R.id.ll_item_mine_head_spsc).setOnClickListener(this);
        view_head.findViewById(R.id.ll_item_mine_head_dpsc).setOnClickListener(this);
        view_head.findViewById(R.id.ll_item_mine_head_zxsc).setOnClickListener(this);
        view_head.findViewById(R.id.ll_item_mine_head_lljl).setOnClickListener(this);
        view_head.findViewById(R.id.ll_item_mine_head_wzby).setOnClickListener(this);
        view_head.findViewById(R.id.ll_item_mine_head_yhts).setOnClickListener(this);
        view_head.findViewById(R.id.ll_item_mine_head_dzgl).setOnClickListener(this);
        view_head.findViewById(R.id.ll_item_mine_head_khfw).setOnClickListener(this);

    }

    //请忽略
    @Override
    public void bannerSuccess(HomeBannerEntity entity) {

    }
    //请忽略
    @Override
    public void recommendSuccess(HomeRecommendEntity entity) {

    }

    //底部商品返回接口
    @Override
    public void goodsSuccess(HomeGoodsEntity entity) {
        mineAdapter.setNewData(entity.values);
    }
    //优惠券返回接口
    @Override
    public void couponlistSuccess(CouponListEntity entity) {
        if ("1".equals(entity.statues)){
            tv_item_mine_head_yhj.setText("" + entity.values.size());
        }
    }

    //请忽略
    @Override
    public void UpgradeSuccess(UpgradeEntity entity) {

    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_mine_message://右上角气泡
                Toast.makeText(getActivity(), "气泡", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_mine_qr://右上角二维码
                Intent zxing = new Intent(getActivity(), FragmentActivity.class);
                zxing.putExtra("type", "zxing");
                getActivity().startActivity(zxing);
                break;
            case R.id.iv_mine_setting://右上角齿轮
                Intent setting = new Intent(getActivity(), FragmentActivity.class);
                setting.putExtra("type", "setting");
                getActivity().startActivity(setting);
                break;
            case R.id.btn_item_mine_head_login://登录按钮
                Intent login = new Intent(getActivity(), FragmentActivity.class);
                login.putExtra("type", "login");
                getActivity().startActivity(login);
                break;
            case R.id.iv_item_mine_head_user_icon://用户头像
                Intent update_user = new Intent(getActivity(), FragmentActivity.class);
                update_user.putExtra("type", "update_user");
                getActivity().startActivity(update_user);
                break;
            case R.id.tv_item_mine_head_user_nick://用户昵称
                Toast.makeText(getActivity(), "用户昵称", Toast.LENGTH_SHORT).show();
                break;
            case R.id.rl_item_mine_head_create_order://我的订单
                Intent create_order = new Intent(getActivity(), FragmentActivity.class);
                create_order.putExtra("type", "create_order");
                getActivity().startActivity(create_order);
                break;
            case R.id.ll_item_mine_head_yfk://预付款
                Toast.makeText(getActivity(), "预付款", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_item_mine_head_dfh://待发货
                Toast.makeText(getActivity(), "待发货", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_item_mine_head_dsh://待收货
                Toast.makeText(getActivity(), "待收货", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_item_mine_head_sdyl://晒单有礼
                Toast.makeText(getActivity(), "晒单有礼", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_item_mine_head_thh://退/换货
                Toast.makeText(getActivity(), "退/换货", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_item_mine_head_share_cash://分享赚现金
                Toast.makeText(getActivity(), "分享赚现金", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_item_mine_head_login_details://登录了解详情有货有赚详情
                Toast.makeText(getActivity(), "登录了解详情有货有赚详情", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_item_mine_head_had_num://出售数量
                Toast.makeText(getActivity(), "出售数量", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_item_mine_head_my_income://我的收入
                Toast.makeText(getActivity(), "我的收入", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_item_mine_head_yhj://优惠券
                Intent coupon_list = new Intent(getActivity(), FragmentActivity.class);
                coupon_list.putExtra("type", "coupon_list");
                getActivity().startActivity(coupon_list);
                break;
            case R.id.ll_item_mine_head_yhb://有货币
                Toast.makeText(getActivity(), "有货币", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_item_mine_head_xgm://限购码
                Toast.makeText(getActivity(), "限购码", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_item_mine_head_lpk://礼品卡
                Toast.makeText(getActivity(), "礼品卡", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_item_mine_head_spsc://商品收藏
                Intent collection_list = new Intent(getActivity(), FragmentActivity.class);
                collection_list.putExtra("type", "collection_list");
                getActivity().startActivity(collection_list);
                break;
            case R.id.ll_item_mine_head_dpsc://店铺收藏
                Toast.makeText(getActivity(), "店铺收藏", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_item_mine_head_zxsc://咨询收藏
                Toast.makeText(getActivity(), "咨询收藏", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_item_mine_head_lljl://浏览记录
                Toast.makeText(getActivity(), "浏览记录", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_item_mine_head_wzby://五折包邮
                Toast.makeText(getActivity(), "五折包邮", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_item_mine_head_yhts://有货推手
                Toast.makeText(getActivity(), "有货推手", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_item_mine_head_dzgl://地址管理
                Toast.makeText(getActivity(), "地址管理", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_item_mine_head_khfw://客户服务
                Toast.makeText(getActivity(), "客户服务", Toast.LENGTH_SHORT).show();
                break;
        }

    }

    //RecyclerView滑动监听器
    private class HomeRecyclerListener extends RecyclerView.OnScrollListener {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            totalY += dy;
            float alpah = (float) totalY / (float) alphaHeight;
            //alpha 0-1;
            if (alpah > 0.8) {
                alpah = 0.8f;
            }
            if (alpah <= 0) {
                alpah = 0;
            }
            ll_mine.setAlpha(alpah);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        /**
         * 用户信息
         */
        Cursor user_cursor = USerService.getInstance().query("user", null, null);
        if (user_cursor.moveToNext()){
            btn_item_mine_head_login.setVisibility(View.GONE);
            cv_item_mine_head.setVisibility(View.VISIBLE);
            rl_item_mine_head_top.setBackgroundResource(R.drawable.view_mine_center_top_bg);

            Glide.with(getContext()).load(API.BaseUrl + user_cursor.getString(user_cursor.getColumnIndex("user_head")))
                    .asBitmap()
                    .centerCrop()
                    //禁用磁盘缓存
                    .diskCacheStrategy( DiskCacheStrategy.NONE)
                    //跳过内存缓存
                    .skipMemoryCache(true)
                    .into(iv_item_mine_head_user_icon);
            tv_item_mine_head_user_nick.setText(user_cursor.getString(user_cursor.getColumnIndex("nick_name")));
            user_id = user_cursor.getString(user_cursor.getColumnIndex("user_id"));
            p.couponList(user_id);//请求优惠券
        }else{
            rl_item_mine_head_top.setBackgroundResource(R.drawable.mine_banner_boy);
            cv_item_mine_head.setVisibility(View.GONE);
            btn_item_mine_head_login.setVisibility(View.VISIBLE);
        }
        /**
         * 收藏的商品
         */
        int spsc_num = 0;
        Cursor spsc_cursor = USerService.getInstance().query("collection", null, null);
        while (spsc_cursor.moveToNext()){
            spsc_num++;
        }
        tv_item_mine_head_spsc.setText("" + spsc_num);
    }


}
